package com.west.imclient.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

import com.west.imclient.view.MeetingRoomView;

import imserverentity.entity.IChattable;
import imserverentity.entity.Protocol;


public class SendActionListener implements ActionListener{
	private MeetingRoomView meetingRoomView;
	private ObjectOutputStream oos;
	
	public SendActionListener(MeetingRoomView meetingRoomView, ObjectOutputStream oos) {
		this.meetingRoomView = meetingRoomView;
		this.oos = oos;
//		System.out.println("Sender OOS: " + oos + " " + this.oos);
	}

	public void actionPerformed(ActionEvent e) {
		String content = meetingRoomView.getJta().getText();
		IChattable from = meetingRoomView.getClient();
		String to = null;
		if(meetingRoomView.getCjl().getSelectedIndex()!=-1) {
			to = (String) meetingRoomView.getCjl().getSelectedValue();
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("from", from);
		params.put("to", to);
		params.put("content", content);
		Protocol protocol = new Protocol("chat", params);
		System.out.println("Sender: " + protocol);

		try {
//			System.out.println("ObjectOutputStream: " + oos);
			oos.writeObject(protocol);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
